﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class SanPhamMua_DAO
    {
          private SqlConnection connection;
        
        public SanPhamMua_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private SanPhamMua getBy(string whereCom, string orderCom)
        {
            SanPhamMua data = null;
            string comStr = "Select [MaSanPham],[TenSanPham],[MaLoaiSanPham],[MaNhaSanXuat],[GiaBan],[KhuyenMai],[GiaDaGiam],[CoHang] from [SanPham]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                
                if (dap.HasData)
                {
                    data = new SanPhamMua();
                    dap.DaReader.Read();

                    data.MaSanPham = dap.DaReader["MaSanPham"].ToString();
                    data.TenSanPham = dap.DaReader["TenSanPham"].ToString();
                    data.MaLoaiSanPham = dap.DaReader["MaLoaiSanPham"].ToString();
                    data.MaNhaSanXuat = dap.DaReader["MaNhaSanXuat"].ToString();
                    data.GiaBan = Convert.ToDouble(dap.DaReader["GiaBan"]);
                    data.KhuyenMai = dap.DaReader["KhuyenMai"].ToString();
                    data.GiaDaGiam = Convert.ToDouble(dap.DaReader["GiaDaGiam"]);
                    data.CoHang = Convert.ToBoolean(dap.DaReader["CoHang"]);
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public SanPhamMua getByKey(string MaSanPham)
        {
            return getBy(" [MaSanPham]='" + MaSanPham + "'", " [MaSanPham] asc");
        }
    }
}